package com.jpyl.jpMedicalTreatment.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Window;

import com.example.sdkumeng.share.ShareHelper;
import com.forxindu.kit.box.PreferenceKit;
import com.forxindu.kit.box.ViewKit;
import com.jpyl.jpMedicalTreatment.common.Constant;
import com.jpyl.jpMedicalTreatment.common.MedicalSettings;
import com.jpyl.jpMedicalTreatment.model.OlderStateModel;
import com.jpyl.jpMedicalTreatment.model.ResponseModel;
import com.jpyl.jpMedicalTreatment.network.Network;
import com.jpyl.jpMedicalTreatment.network.api.ApiError;
import com.jpyl.jpMedicalTreatment.network.api.RequestApi;
import com.jpyl.jpMedicalTreatment.view.activity.BaseActivity;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/5/24.
 */

public class WXPayEntryActivity extends FragmentActivity implements IWXAPIEventHandler {

    public static final String TAG = WXPayEntryActivity.class.getSimpleName();
    final RequestApi publicApi = Network.getRequestApi();
    private IWXAPI wxAPI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        wxAPI = WXAPIFactory.createWXAPI(this, Constant.APP_ID);
        wxAPI.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        wxAPI.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            Log.e("Activity","===resp.errCode===="+resp.errCode);
            switch (resp.errCode) {
                case 0:
                    //支付成功
                    String orderId = PreferenceKit.getSharedPreference(WXPayEntryActivity.this, MedicalSettings.ORDER_TRADE_NUMBER, "");
                    getResult(orderId);
                    break;
                case -1:
                    ViewKit.showToast(WXPayEntryActivity.this, "支付失败");
                    finish();
                    //支付失败
                    break;
                case -2:
                    //取消支付
                    ViewKit.showToast(WXPayEntryActivity.this, "支付取消");
                    finish();
                    break;
                default:
                    break;
            }
        }
    }

    //获取微信返回给客服端的借口
    private void getResult(String orderTradeNumber) {

        publicApi.jpylCheckOrderPaySuccess(orderTradeNumber).map(new Func1<ResponseModel<OlderStateModel>,
                OlderStateModel>() {
            @Override
            public OlderStateModel call(ResponseModel<OlderStateModel> responseModel) {
                if (responseModel.getCode() != 0) {
                    String msg = responseModel.getMsg();
                    throw new ApiError(responseModel.getCode(), msg);
                }
                return responseModel.getData();
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<OlderStateModel>() {
                    @Override
                    public void onCompleted() {
                        ViewKit.showToast(WXPayEntryActivity.this, "支付失败");
                        finish();
                    }

                    @Override
                    public void onError(Throwable e) {
//                        e.printStackTrace();
                            if (e instanceof ApiError) {
                                ApiError error = (ApiError) e;
                                ViewKit.showToast(WXPayEntryActivity.this, error.getMsg() + "");
                            }else {
                                ViewKit.showToast(WXPayEntryActivity.this, "支付失败");
                            }
                    }

                    @Override
                    public void onNext(OlderStateModel model) {
                        if (model.isSuccess()) {
                            //支付成功
                            ViewKit.showToast(WXPayEntryActivity.this, "支付成功");
                        } else {
                            //支付失败
                            ViewKit.showToast(WXPayEntryActivity.this, "支付失败");
                        }
                        finish();
                    }
                });
    }
}
